#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "switchbutton.h"
#include "setting.h"
#include <QDebug>
#include <QIcon>
#include <QPixmap>

MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setting = new Setting;
    serialPort = new QSerialPort;
    serialPort = setting->getSerialPort();

    connect(ui->widget_button_red, &SwitchButton::statusChanged, this, &MainWindow::switchButton_R);//红灯的状态
    connect(ui->widget_button_green, &SwitchButton::statusChanged, this, &MainWindow::switchButton_G);//绿灯的状态
    connect(ui->widget_button_blue, &SwitchButton::statusChanged, this, &MainWindow::switchButton_B);//蓝灯的状态
    connect(ui->widget_button_beep, &SwitchButton::statusChanged, this, &MainWindow::switchButton_BEEP);//beep的状态
    connect(ui->widget_button_people, &SwitchButton::statusChanged, this, &MainWindow::switchButton_P);//人体红外的状态
    connect(ui->widget_button_engin, &SwitchButton::statusChanged, this, &MainWindow::switchButton_E);//舵机的状态
}

MainWindow::~MainWindow()
{
    delete ui;
}

//返回红灯按钮的状态
void MainWindow::switchButton_R(bool checked)
{
    if(checked == true)
    {
        ui->LED_R->setStyleSheet("QLabel{"
                                       "background-image:url(:/res/redLED.png);"
                                       "background-position:top right;"
                                       "background-origin:content;"
                                       "background-repeat:none;"
                                       "}");
        if(serialPort->isOpen())
        {
            QString sendData = "0";
            sendData.append("\r\n");
            QByteArray sendDataArray = sendData.toUtf8();
            serialPort->write(sendDataArray);
        }
    }
    else
    {
        ui->LED_R->setStyleSheet("QLabel{"
                                       "background-image:url(:/res/blackLED.png);"
                                       "background-position:top right;"
                                       "background-origin:content;"
                                       "background-repeat:none;"
                                       "}");
//        if(serialPort->isOpen())
//        {
//            QString sendData = "1";
//            sendData.append("\r\n");
//            QByteArray sendDataArray = sendData.toUtf8();
//            serialPort->write(sendDataArray);
//        }
    }
    qDebug()<<"LED_R:"<<checked;
}

void MainWindow::switchButton_G(bool checked)
{
    if(checked == true)
    {

        ui->LED_G->setStyleSheet("QLabel{"
                                       "background-image:url(:/res/greenLED.png);"
                                       "background-position:top right;"
                                       "background-origin:content;"
                                       "background-repeat:none;"
                                       "}");
    }
    else
    {
        ui->LED_G->setStyleSheet("QLabel{"
                                       "background-image:url(:/res/blackLED.png);"
                                       "background-position:top right;"
                                       "background-origin:content;"
                                       "background-repeat:none;"
                                       "}");
    }
}

void MainWindow::switchButton_B(bool checked)
{
    if(checked == true)
    {
        ui->LED_B->setStyleSheet("QLabel{"
                                       "background-image:url(:/res/blueLED.png);"
                                       "background-position:top right;"
                                       "background-origin:content;"
                                       "background-repeat:none;"
                                       "}");
    }
    else
    {
        ui->LED_B->setStyleSheet("QLabel{"
                                       "background-image:url(:/res/blackLED.png);"
                                       "background-position:top right;"
                                       "background-origin:content;"
                                       "background-repeat:none;"
                                       "}");
    }
}

void MainWindow::switchButton_BEEP(bool checked)
{
    if(checked == true)
    {
        ui->BEEP_label->setStyleSheet("QLabel{"
                                       "background-image:url(:/res/beep_ON.png);"
                                       "background-position:top right;"
                                       "background-origin:content;"
                                       "background-repeat:none;"
                                       "}");
    }
    else
    {
        ui->BEEP_label->setStyleSheet("QLabel{"
                                       "background-image:url(:/res/beep_OFF.png);"
                                       "background-position:top right;"
                                       "background-origin:content;"
                                       "background-repeat:none;"
                                       "}");
    }
}

void MainWindow::switchButton_P(bool checked)
{
    qDebug()<<checked;
}

void MainWindow::switchButton_E(bool checked)
{
    if(checked == true)
    {

        ui->Engine_label->setStyleSheet("QLabel{"
                                       "background-image:url(:/res/engine _ON.png);"
                                       "background-position:top right;"
                                       "background-origin:content;"
                                       "background-repeat:none;"
                                       "}");
    }
    else
    {
        ui->Engine_label->setStyleSheet("QLabel{"
                                       "background-image:url(:/res/engine_OFF.png);"
                                       "background-position:top right;"
                                       "background-origin:content;"
                                       "background-repeat:none;"
                                       "}");
    }
}

void MainWindow::on_btn_setting_clicked()
{
    Setting *setting = new Setting;
    setting->show();
    //this->close();
}
